package com.scuola.scuoladocente;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class listaAlunniDocente extends Activity {

	private ListView lv;
	private JSONArray parsedList = null;
	private JSONParser parser = new JSONParser();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_alunni_docente);
        
        TextView tv_lad_info = (TextView)findViewById(R.id.tv_lad_info);
        tv_lad_info.setText(Html.fromHtml("<b>Classe: </b>"+getIntent().getStringExtra("nomeClasse")
        		+" - <b>"+getIntent().getStringExtra("nomeMateria")+"</b>"));
        
        lv = (ListView)findViewById(R.id.lv_lad_listaalunni);
        ArrayList<String> lvItems = new ArrayList<String>();
        
        try {
        	AsyncTask<Void, Void, String> getAlumniByClassIdResponse = new getAlumniByClassId().execute();
			parsedList = (JSONArray) parser.parse(getAlumniByClassIdResponse.get());
			
			for(int i = 0; i < parsedList.size(); i++ ){
				JSONObject obj = (JSONObject) parser.parse(parsedList.get(i).toString());
				lvItems.add(obj.get("cognome").toString()+" "+obj.get("nome").toString());
			}
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvItems);
			lv.setAdapter(adapter);
			
			lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
					try {
						JSONObject selectedObj = (JSONObject) parser.parse(parsedList.get(arg2).toString());
						Intent VotoAlunno = new Intent(getApplicationContext(), VotoAlunno.class);
						VotoAlunno.putExtra("idAlunno", selectedObj.get("id").toString());
						VotoAlunno.putExtra("cognome", selectedObj.get("cognome").toString());
						VotoAlunno.putExtra("nome", selectedObj.get("nome").toString());
						VotoAlunno.putExtra("nomeMateria", getIntent().getStringExtra("nomeMateria").toString());
						VotoAlunno.putExtra("idMateria", getIntent().getStringExtra("idMateria").toString());
						VotoAlunno.putExtra("idClasse", getIntent().getStringExtra("idclasse").toString());
						VotoAlunno.putExtra("idDocente", getIntent().getStringExtra("idutente").toString());
			           	startActivityForResult(VotoAlunno, 0);
			           	
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			
	    } catch (Exception e) {
			e.printStackTrace();
		}
    }
        
    private class getAlumniByClassId extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... arg0) {
			try{	
				LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
				hm.put("classId", getIntent().getStringExtra("idclasse"));
				String strResponse = operations.wsCall("getAlumniByClassId", hm).toString();
				return strResponse;
				
			} catch (Exception e) {
				System.err.println("getAlumniByClassId errore (Exception)");
				e.printStackTrace();
				return null;
			}
		
		}
    }

    
}
